<?php

class m_detail_order extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    public function get_pembelian($where = null, $id = null, $limit = null, $offset = null)
    {
        $this->db->select("detail_order.*");
        
        $this->db->select("order_barang.ID_ORDER");
        $this->db->select("order_barang.TGL_ORDER");
        $this->db->select("order_barang.NOMER_ORDER");

        $this->db->select('barang.ID_BARANG');
        $this->db->select('barang.NAMA_BARANG');
        $this->db->select('barang.HARGA_JUAL');
        $this->db->select('barang.NAMA_SATUAN');

        $this->db->select("faktur.ID_FAKTUR");
        $this->db->select("faktur.NOMER_FAKTUR");
        $this->db->select("faktur.TGL_FAKTUR");

        $this->db->join("barang","barang.ID_BARANG = detail_order.ID_BARANG");
        $this->db->join("order_barang","order_barang.ID_ORDER = detail_order.ID_ORDER");
        $this->db->join("faktur","faktur.ID_FAKTUR = order_barang.ID_FAKTUR");

        if($where != null) $this->db->where($where,$id);
        if($limit != null && $offset != null) $this->db->limit($limit, $offset);
        
        return $this->db->get("detail_order");
    }

    public function get($where = null, $id = null, $limit = null, $offset = null)
    {
        if($limit != null && $offset != null) $this->db->limit($limit, $offset);
        $this->db->select("detail_order.*");
        $this->db->select("barang.NAMA_BARANG,barang.HARGA_BELI,barang.NAMA_SATUAN");
        $this->db->join("barang","barang.ID_BARANG = detail_order.ID_BARANG");
        if($where != null) $this->db->where($where,$id);
        return $this->db->get("detail_order");
    }

    public function get_detail($where = null, $limit = null, $offset = null)
    {
        $this->load->model('crud/m_order_barang');
        $id_order = $this->m_order_barang->get_id($where)->row();

        if($where != null) $this->db->where('ID_ORDER',$id_order->ID_ORDER);
        if($limit != null && $offset != null) $this->db->limit($limit, $offset);
        return $this->db->get("detail_order");
    }

    function get_all($limit, $uri)
    {
        $result = $this->db->get('order_beli', $limit, $uri);
        if ($result->num_rows() > 0) {
            return $result->result_array();
        } else {
            return array();
        }
    }

    function get_one($id)
    {
        $this->db->where('ID_DETAIL_ORDER', $id);
        $result = $this->db->get('detail_order');
        if ($result->num_rows() == 1) {
            return $result;
        } else {
            return array();
        }
    }

    function insert($id_order) {

        $data = array(
                'ID_BARANG' => $this->input->post('ID_BARANG', TRUE),
                'ID_ORDER' => $id_order,
                'JUMLAH' => $this->input->post('UNIT', TRUE),
                'TOTAL_HARGA' => $this->input->post('HARGA', TRUE),
        );
        return $this->db->insert('detail_order', $data);
    }

    function update($id) {
        $data = array(

                'ID_SUPPLIER' => $this->input->post('ID_SUPPLIER', TRUE),

                'NAMA_SUPPLIER' => $this->input->post('NAMA_SUPPLIER', TRUE),

                'TGL_ORDER' => $this->input->post('TGL_ORDER', TRUE),

        );
        $this->db->where('NOMER_ORDER', $id);
        return $this->db->update('detail_order', $data);
    }

    function realisasi($id_order,$id_faktur) {
        $data = array(
                'ID_FAKTUR' => $id_faktur
        );

        $this->db->where('ID_ORDER', $id_order);
        return $this->db->update('detail_order', $data);
    }

    function delete($id) {
        $this->db->where('NOMER_ORDER', $id);
        $this->db->delete('detail_order');
    }

    function deletedetail($iddetailorder) {
        $this->db->where('ID_DETAIL_ORDER', $iddetailorder);
        $this->db->delete('detail_order');
    }

}
?>
